//
//  BNDPreference.m
//  OfficeDoc
//
//  Created by pie on 2022/3/30.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDPreference.h"
#import "BndUser.h"
#import "BNDOfficeDocDetailsVc.h"
#import "BNDHomeController.h"
#import "BNDOfficeDocVc.h"
#import "BNDOfficeDocKit.h"

@interface BNDPreference()
@property (strong,nonatomic) OSSClient *client;  //oss上传管理
@property (strong,nonatomic) BndUser *userInfo;  //用户信息
@end

@implementation BNDPreference

static BNDPreference *_instance;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

- (instancetype)init
{
    self = [super init];
    if(self) {
        
    }
    
    return self;
}



+ (void)hanOSSUploadFile
{
    [BNDApiManager officeDocRequestFileUrl:^(NSString * _Nonnull accessKeyId, NSString * _Nonnull securityToken, NSString * _Nonnull accessKeySecret, NSString * _Nonnull expiration) {
        NSString *endpoint = @"http://oss-cn-shenzhen.aliyuncs.com";
        // 移动端建议使用STS方式初始化OSSClient。
        id<OSSCredentialProvider> credential = [[OSSFederationCredentialProvider alloc] initWithFederationTokenGetter:^OSSFederationToken * _Nullable{
            OSSFederationToken *token = [OSSFederationToken new];
            // 从STS服务获取的临时访问密钥（AccessKey ID和AccessKey Secret）。
            token.tAccessKey = accessKeyId;
            token.tSecretKey = accessKeySecret;
            // 从STS服务获取的安全令牌（SecurityToken）。
            
            token.tToken =securityToken;
            // 临时访问凭证的过期时间。
            token.expirationTimeInGMTFormat = expiration;
            return token;
        }];
        
        [BNDPreference sharedInstance].client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
    } failure:^(NSError * _Nullable error) {
        
    }];
    
}


+ (void)handleCompanyInfo;
{
    [BNDApiManager officeDocRequestFindCompanyInfoById:^(NSString * _Nonnull companyName) {
        if([companyName isEqual:[NSNull null]]){
            [BNDPreference sharedInstance].companyName = @"";
        }else{
            [BNDPreference sharedInstance].companyName = companyName;
        }
    } failure:^(NSError * _Nullable error) {
        
    }];
    
}

+ (void)handleUserInfo
{
    [BNDApiManager  officeDocRequestUserInfoVoByIds:^(NSArray<BndUser *> * _Nonnull moreClass) {
        if(moreClass.count>0){
            [BNDPreference sharedInstance].userInfo = moreClass[0];
        }
        
    } failure:^(NSError * _Nullable error) {
        
    }];
    
    [BNDApiManager officeDocRequestUserInfo:^(BOOL isManage) {
        [BNDPreference sharedInstance].isAdmin = isManage;
    } failure:^(NSError * _Nullable error) {
        
    }];
}


+ (void)handleOfficeDocDetailsVc:(NSString * _Nullable )officeDocId
{
    [BNDOfficeDocDetailsVc bndPushOfficeDocDetailsVc:[BNDPushVCHandler topViewController] officeDocId:officeDocId];
}


+ (void)handlePushHomeVc
{
    BNDHomeController *vc = [[BNDHomeController alloc] init];
    [[BNDPushVCHandler currentSelectedNavController] pushViewController:vc animated:YES];
}

- (NSString*)handleProduceImageUrl:(NSString* _Nullable)objectKey;
{
    if([objectKey isEqual:[NSNull null]]){
        return  @"";
    }
    
    if(objectKey.length ==0){
        return  @"";
    }
    
    return  objectKey;
}

+ (void)handleOfficeDocListVc
{
    BNDOfficeDocVc *vc = [[BNDOfficeDocVc alloc] init];
    [[BNDPushVCHandler currentSelectedNavController] pushViewController:vc animated:YES];
}

+ (NSString*)handleFileImageName:(NSString*)fileType
{
    if([fileType isEqualToString:@"doc"]){
        return  @"cd_icons_doc";
    }else if([fileType isEqualToString:@"docx"]){
        return  @"cd_icons_doc";
    }
    else if([fileType isEqualToString:@"pdf"]){
        return  @"cd_icons_pdf";
    }
    else if([fileType isEqualToString:@"xls"]){
        return  @"cd_icons_xls";
    }
    else if([fileType isEqualToString:@"xlsx"]){
        return  @"cd_icons_xls";
    }
    else if([fileType isEqualToString:@"ppt"]){
        return  @"cd_icons_ppt";
    }
    else if([fileType isEqualToString:@"pptx"]){
        return  @"cd_icons_ppt";
    }
    return  @"cd_icons_zip";
}

+ (UIImage*)getLocalImage:(NSString*)string
{
    if (string.length>0) {
        
        NSBundle *currentBundle = [NSBundle bundleForClass:[[BNDPushVCHandler topViewController] class]];
        NSURL *url = [currentBundle URLForResource:@"OfficeDocSdk" withExtension:@"bundle"];
        NSBundle *targetBundle = [NSBundle bundleWithURL:url];
        
        UIImage * image =[UIImage imageNamed:string
                                    inBundle:targetBundle
               compatibleWithTraitCollection:nil];
        
        return image;
    }
    return nil;
}

@end
